/* File makes Table 10 */

/* 	Input files: 	declarations, temp_follows
	Output files: T10_declarations_expost.tex */

**********************************************
clear all
macro drop _all
scalar drive		= "E:"
scalar maindir		= "`=drive'\Replicate"
scalar tabledir		= "`=drive'\Replicate\TablesRep"
cd "`=maindir'"
**********************************************

use declarations, clear  

egen usersymb=group(userid symbolid)
xtset usersymb date
forval i=11/30 {
gen bull_f`i'=f`i'.bull
gen bear_f`i'=f`i'.bear
}

egen bulldays1130=rowtotal(bull_f11 bull_f12 bull_f13 bull_f14 bull_f15 bull_f16 bull_f17 bull_f18 bull_f19 bull_f20 bull_f21 bull_f22 bull_f23 bull_f24 bull_f25 bull_f26 bull_f27 bull_f28 bull_f29 bull_f30)  
egen beardays1130=rowtotal(bear_f11 bear_f12 bear_f13 bear_f14 bear_f15 bear_f16 bear_f17 bear_f18 bear_f19 bear_f20 bear_f21 bear_f22 bear_f23 bear_f24 bear_f25 bear_f26 bear_f27 bear_f28 bear_f29 bear_f30)  
gen decnetbull1130=cond(bulldays>beardays,1,0)
gen decnetbear1130=cond(beardays>bulldays,1,0)

gen netbulldays=bulldays-beardays
gen netbeardays=beardays-bulldays

drop ym usersymb bull_f11-bear_f30
compress
save temp_declare, replace




use userid symbolid date bull bear f_bull_1_10 f_bear_1_10 using temp_follows, clear
merge 1:1 userid symbolid date using temp_declare, nogen 

drop bulldays1130 beardays1130

scalar lhsvar ="netbulldays"

eststo bull0: reghdfe `=lhsvar' bull , absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `=lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)

keep if e(sample)==1

eststo bull1: reghdfe `=lhsvar' bull 1.bull#1.f_bull_1_10 , absorb(i.userid i.symbolid#i.date) vce(cl userid)  
	qui sum `=lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)


scalar lhsvar= "decnetbull1130"

eststo bull2: reghdfe `=lhsvar' bull, absorb(i.userid i.symbolid#i.date) vce(cl userid)  
	qui sum `=lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	
eststo bull3: reghdfe `=lhsvar' bull 1.bull#1.f_bull_1_10 , absorb(i.userid i.symbolid#i.date) vce(cl userid)  
	qui sum `=lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)


*****************************************************************************************************

cap estadd local dsFe "Y" : *
cap estadd local usyfE "Y" : *

#delimit ; 
local stats_opts " stats(N N_clust r2 avg usyfE dsFe, fmt(%18.0fc %18.0fc %12.2fc %12.2fc %12.0fc %12.0fc) labels("\# observations" "\# clusters (users)" "$\textit{R}^2$" "Mean of dependent var.($\%$)" "User FE" "Day x stock FE") ) " ;

local opts  "b(%16.2fc) se(%16.2fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines nomtitles mgroups(
"Dep. var.: \# net Bull days\textsubscript{s,(t+11 $\rightarrow$ t+30)}"
"$\mathbbm{1}$ if Net Bull\textsubscript{s,(t+11 $\rightarrow$ t+30)}" 
, pattern(1 0 1 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;   

local filename "T10_declarations_expost" ;

cd "`=tabledir'" ;
esttab bull0 bull1 bull2 bull3 using `filename'.tex, `opts' `stats_opts' 
drop(_cons) coef(
bull 	"$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t}" 
bear 	"$\mathbbm{1}$ Declare Bear\textsubscript{i,s,t}"
1.bull#1.f_bull_1_10 "$\mathbbm{1}$ Bull\textsubscript{i,s,t} x $\mathbbm{1}$ Net Bull follows\textsubscript{i,s,(t+1 $\rightarrow$ 10)}" 
1.bear#1.f_bear_1_10 "$\mathbbm{1}$ Bear\textsubscript{i,s,t} x $\mathbbm{1}$ Net Bear follows\textsubscript{(i,s,t+1 $\rightarrow$ 10)}" 
) replace ;
#delimit cr 
cd "`=maindir'" 


cap n erase temp_declare.dta




